
import locker from './lock'
import { getCodeCorrDictList } from '@/assets/request/api' 
const getKey = (codeType, condition) => {
  let key = codeType;
  if (condition) {
    for (let p in condition) {
      key += "_" + p + "_" + (condition[p] || '');
    }
  }
  return key;
}

const codeProvider = {
  registerCodeType(codeType, condition) {
    return new Promise((resolve, reject) => {
      let key = getKey('code_' + codeType, condition);
      let codeData = _g.getCacheData(key);
      codeData = codeData ? codeData.data : null;
      if (codeData) {
        resolve(codeData);
      }
      locker.lock(key, () => {
        console.log(key,"加锁")
        let codeData = _g.getCacheData(key);
        codeData = codeData ? codeData.data : null;
        if (codeData) {
          resolve(codeData);
          locker.releaseLock(key);  //release lock
        }
        console.log(codeType, 'req from server!');
        this._getCodeList(codeType, condition).then(resp => {
          if (resp) {
            let cacheTimeout = resp.cacheTimeout || 0;
            // 0 代表永久
            if (cacheTimeout) {
              _g.putCacheData(getKey('code_' + codeType, condition), { data: resp, requestState: false }, parseInt(cacheTimeout));
            } else {
              _g.putCacheData(getKey('code_' + codeType, condition), { data: resp, requestState: false });
            }
          }
          locker.releaseLock(key);
          resolve(resp);
        }).catch(error=>{
          locker.releaseLock(key);
          reject(error);
        });
        console.log(key,"释放锁")
      });
    });
  },
  _getCodeList(codeType, condition) {
    return new Promise((resolve, reject) => {
      const _data = {
        codeType: codeType
      };
      Object.assign(_data, condition);
      getCodeCorrDictList(_data).then(res => {
        resolve(res.data)
      }).catch(err => {
        _g.toastMsg('error', err.message);
        reject(err);
      })
    });
  },
  getList(codeType, condition = {}) {
    return new Promise((resolve, reject) => {
      let codeData = _g.getCacheData(getKey('code_' + codeType, condition));
      codeData = codeData ? codeData.data : null;
      let result = null;
      if (!codeData) {
        this.registerCodeType(codeType, condition).then(resp => {
          result = this.parseData(resp);
          resolve(result);
        });
        return;
      }
      result = this.parseData(codeData);
      resolve(result);
      return;
    });
  },
  parseData(codeData) {//if tree return tree ,if seq return list;
    if(!codeData) {
      return
    };
    let seqType = codeData.seqType;
    let result = {};
    if (seqType == 'tree' || seqType == 'tree_pojo') {//树类型
      return codeData;
    } else {
      let codeList = codeData.codeList;
      let textField = codeData.textField || 'text';
      let valueField = codeData.valueField || 'value';
      // 数据格式：
      // {
      //   {
      //     "text": "基础配置",
      //     "value": "1",
      //     "other": {
      //         "id": 18,
      //         "dictCode": "CODE_MODEL_CATEGROIES",
      //         "dictName": "基础配置",
      //         "dictValue": "1",
      //         "orderBy": 10,
      //         "parentId": 17,
      //         "parentIds": "0,1,17",
      //         "type": 3,
      //         "remark": null,
      //         "createId": 36,
      //         "createTime": "2023-02-22T20:07:34",
      //         "modifyId": 36,
      //         "modifyTime": "2023-02-21T14:12:28"
      //     }
      //   },{
      //     "text": "基础配置",
      //     "value": "1",
      //     "other": {
      //         "id": 18,
      //         "dictCode": "CODE_MODEL_CATEGROIES",
      //         "dictName": "基础配置",
      //         "dictValue": "1",
      //         "orderBy": 10,
      //         "parentId": 17,
      //         "parentIds": "0,1,17",
      //         "type": 3,
      //         "remark": null,
      //         "createId": 36,
      //         "createTime": "2023-02-22T20:07:34",
      //         "modifyId": 36,
      //         "modifyTime": "2023-02-21T14:12:28"
      //     }
      //   }
      // }
      result = _.map(codeList || [], item => { return { text: item[textField], value: item[valueField], other: item } });
      return result;
    }
  },
}
export {
  codeProvider
}